package com.jpush;

import com.jpush.pojo.Person;
import com.jpush.visitor.LogVisitor;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class AppendMain {
    public static void main(String[] args) throws IOException {
        // 修改源class ，利用asm 在字节码层面修改class
        // 这里加载指定的class
        // 修改指定的class 的特定方法
        ClassReader classReader = new ClassReader(Person.class.getName());
        ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS);
        ClassVisitor classVisitor = new LogVisitor(Opcodes.ASM5, classWriter);
        classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES);
        byte[] bytecode = classWriter.toByteArray();

        // 修改后的结果
        try(FileOutputStream fos = new FileOutputStream(
                new File("/Users/liyh/dev/GitSpace/asm-study/target/Person.class"))){

            fos.write(bytecode);
            fos.flush();
        }
    }
}
